package com.yipao.admin.web.goods;

import com.yipao.admin.annotation.RequiresPermissionsDesc;
import com.yipao.core.constant.GlobalConstant;
import com.yipao.core.util.ResponseUtil;
import com.yipao.core.validator.Order;
import com.yipao.core.validator.Sort;
import com.yipao.db.domain.goods.GoodsFormula;
import com.yipao.db.service.goods.GoodsFormulaService;
import com.yipao.db.service.goods.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 商品配方
 *
 * @author tianhao
 * @date 2020/4/29 18:27
 **/
@RestController
@RequestMapping("/admin/goods/formula")
@Validated
public class AdminGoodsFormulaController {
    @Resource
    private GoodsFormulaService goodsFormulaService;
    @Resource
    private GoodsService goodsService;

    /**
     * 删除商品配方
     *
     * @param
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("admin:goods:formula:delete")
    @RequiresPermissionsDesc(menu = {"商品管理", "配方管理"}, button = "删除")
    public Object delete(@PathVariable("id") Integer id) {
        // 配方被原料使用,不能删除
        int count = goodsService.queryCountGoodsByFormulaId(id);
        if (count > 0) {
            return ResponseUtil.fail(ResponseUtil.STATUS_CODE_UN_AUTH, "删除失败,请先删除对应的商品");
        }
        // 根据配方id,查询商品
        goodsFormulaService.delete(id);
        return ResponseUtil.ok();
    }

    /**
     * 新增商品配方
     *
     * @param goodsFormula
     * @return
     */
    @PostMapping("/insert")
    @RequiresPermissions("admin:goods:formula:insert")
    @RequiresPermissionsDesc(menu = {"商品管理", "配方管理"}, button = "新增")
    public Object insert(@RequestBody GoodsFormula goodsFormula) {
        goodsFormulaService.insert(goodsFormula);
        return ResponseUtil.ok(goodsFormula);
    }

    /**
     * 修改商品配方
     *
     * @param goodsFormula
     * @return
     */
    @RequiresPermissions("admin:goods:formula:update")
    @RequiresPermissionsDesc(menu = {"商品管理", "配方管理"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody GoodsFormula goodsFormula) {
        goodsFormulaService.update(goodsFormula);
        return ResponseUtil.ok(goodsFormula);
    }

    /**
     * 配方管理列表
     *
     * @param goodsName 产品名称
     * @param page      第几页
     * @param limit     每页显示的记录数
     * @param sort      排序字段,默认创建时间
     * @param order     排序规则,默认降序,即先创建的,排在前面
     * @return
     */
    @RequiresPermissions("admin:goods:formula:list")
    @RequiresPermissionsDesc(menu = {"商品管理", "配方管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String goodsName,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return ResponseUtil.okList(goodsFormulaService.adminList(goodsName, page, limit, sort, order));
    }

    /**
     * 商品配方详情
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:goods:formula:detail")
    @RequiresPermissionsDesc(menu = {"商品管理", "配方管理"}, button = "详情")
    @GetMapping("/detail/{id}")
    public Object detail(@NotNull @PathVariable("id") Integer id) {
        return goodsFormulaService.queryGoodsFormulaByGoodsId(id);
    }

    /**
     * 查询全部配方
     *
     * @return
     */
    @GetMapping("/all")
    @RequiresPermissions("admin:goods:formula:all")
    @RequiresPermissionsDesc(menu = {"商品管理", "配方管理"}, button = "查询全部")
    public Object queryAllFormula() {
        return ResponseUtil.okList(goodsFormulaService.queryAllFormula());
    }

}
